package com.gxa.hotel.admin.realm;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.gxa.hotel.admin.service.IAdminBOService;
import com.gxa.hotel.admin.service.IPermissionBOService;
import com.gxa.hotel.admin.service.IRoleBOService;
import com.gxa.hotel.core.pojo.bo.AdminBO;
import com.gxa.hotel.core.pojo.bo.PermissionBO;
import com.gxa.hotel.core.pojo.bo.RoleBO;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class AdminRealm extends AuthorizingRealm {
    @Autowired
    IPermissionBOService permissionDOService;
    @Autowired
    IRoleBOService roleBOService;
    @Autowired
    IAdminBOService adminBOService;
    private LambdaQueryWrapper<AdminBO> lambda=null;
    //授权方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
       //获取登录手机号
    //获取登录用户名
        String phone = (String) principalCollection.getPrimaryPrincipal();
        System.out.println("授权过程中获取用户的手机号:"+phone);
        //添加角色和权限
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        //查询角色信息 roleList
        LambdaQueryWrapper<RoleBO> lambda= Wrappers.lambdaQuery(RoleBO.class);
        lambda.eq(RoleBO::getPhone,phone);
        List<RoleBO> list = roleBOService.list(lambda);

        for (RoleBO roleBO : list) {
            info.addRole(roleBO.getName());
            //查询权限信息 permissionList
            LambdaQueryWrapper<PermissionBO> lambdap= Wrappers.lambdaQuery(PermissionBO.class);
            lambdap.eq(PermissionBO::getName,roleBO.getName());
            List<PermissionBO> list1 = permissionDOService.list(lambdap);

            for (PermissionBO permissionBO : list1) {

                info.addStringPermission(permissionBO.getUrl());
            }
        }

        //返回已经完成绑定(角色信息和权限信息)的授权信息对象


        return info;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

//        Object principal = authenticationToken.getPrincipal();
        String phone = (String) authenticationToken.getPrincipal();
        System.out.println("认证过程中获取用户的手机号:"+phone);
//        UsernamePasswordToken upToken =(UsernamePasswordToken)authenticationToken;
//        String phone = upToken.getUsername();
        System.out.println(phone);
        lambda= Wrappers.lambdaQuery(AdminBO.class);
        //TODO 通过声明server业务实现根据手机号查询admin用户密码的功能
        lambda.eq(AdminBO::getPhone,phone);

        AdminBO admin = adminBOService.getOne(lambda);

        System.out.println(admin);
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(phone, admin.getPwd(), getName());

        return info;
    }
}
